<?php
include( "inc/header.php" );
login_check( );
$_classArray = array(
			 0 => '企业员工',
			 1 => '公司领导',
			 2 => '合作伙伴'
);
require('../inc/smsConfig.php');
require_once('../inc/sms.php');
if (isset($_POST['action']) && $_POST['action']=='send') {
	//从excel文件中取出手机号码
	$excel_file = $_FILES['importfile'];
	if( $excel_file ) $excel_file = $_FILES['importfile']['tmp_name'];
	if($excel_file != '') $getExcelData = getExcelData($excel_file);
	$f_phones = array();
	if (count($getExcelData)>0) {
		foreach ($getExcelData as $E_key => $E_value) {
			foreach ($E_value as $T_key => $T_value) {
				if ($T_key == 1) { //1表示Excle表中第二列即B列，因模板文件中B列为电话号码
					$f_phones[] = $T_value;
				}
			}
		}
	}
	//从phone表单取出手机号码
	if (substr_count($_POST['phone'],';')>1) { //如果接收的电话号码中包含至少2个';'说明是多方发送
		$p_phones = explode(';',substr(trim($_POST['phone']),0,-1)); //将手机号码转换为数组
	}else {
		$one_phones = strstr($_POST['phone'],';') ? substr(trim($_POST['phone']),0,-1) : trim($_POST['phone']); //表明为单个用户发送
	}
	//从分组中取得手机号码
	if (isset($_POST['class'])) {
		$c_phones = getClassPhone($_POST['class']);
	}
	//组装电话号码数组
	if ((count($f_phones)>0) && (count($p_phones)>0) && (count($c_phones)>0)) {//表明excel、phone表单和分组同时有电话号码
		$phone_str = implode(';',$f_phones).';'.implode(';',$p_phones).';'.implode($c_phones);
		$phones = explode(';',$phone_str);
		unset($phone_str);
	} elseif ((count($f_phones)>0) && (count($p_phones)>0) && (count($c_phones)==0)) {
		$phone_str = implode(';',$f_phones).';'.implode(';',$p_phones);
		$phones = explode(';',$phone_str);
		unset($phone_str);
	} elseif ((count($f_phones)==0) && (count($p_phones)>0) && (count($c_phones)>0)) {
		$phone_str = implode(';',$c_phones).';'.implode(';',$p_phones);
		$phones = explode(';',$phone_str);
		unset($phone_str);
	} elseif ((count($f_phones)>0) && (count($p_phones)==0) && (count($c_phones)>0)) {
		$phone_str = implode(';',$f_phones).';'.implode(';',$c_phones);
		$phones = explode(';',$phone_str);
		unset($phone_str);
	} elseif ((count($f_phones)>0) && (count($p_phones)==0) && (count($c_phones)==0)) {
		$phones = $f_phones;
	} elseif ((count($f_phones)==0) && (count($p_phones)>0) && (count($c_phones)==0)) {
		$phones = $p_phones;
	} elseif ((count($f_phones)==0) && (count($p_phones)==0) && (count($c_phones)>0)) {
		$phones = $c_phones;
	}
	/*手机号码验证，过滤*/
	$phones = checkphone($phones); //验证电话号码是否正确，过滤掉非电话号码
	if (is_array($phones)) {
		$n_phones = isset($one_phones) ? implode(';',$phones).';'.$one_phones : implode(';',$phones); //此处用于phone表单有单个号码的情况
		$phones = array_flip(array_flip(array_filter(explode(';',$n_phones)))); //array_flip用于将数组中的键与值交换，两次重复使用可删除数组中重复的元素，并保留最后一个元素;array_filter(利用回调函数来对数组进行过滤)过滤掉其中的空值
		unset($n_phones);
		//print_r($phones);
	}else {
		$phones = $one_phones;
		//echo $phones;
	}
	//exit;
	//验证电话号码是否存在
	if (empty($phones)) {
		admin_showerrs('接收手机号不能为空', null, 0 );//提示信息
	}
	$content = $_POST['content'];  //短信内容
	if (empty($content)) {
		admin_showerrs('短信内容不能为空', null, 0 );//提示信息
	}
	$db->query("SELECT keyword FROM tun2_smskeywords");
	$dict = array();
	while ($db->next_record()) {
		if (strstr($content,$db->Record['keyword'])) {//表明短信内容中存在黑字典中所不允许的字符串
			$dict[] = $db->Record['keyword'];
		}
	}
	$sms = new sms($sms_config['user'],$sms_config['pwd']); //实例化短信操作类
	if (count($dict) > 0) {
		$msg = '短信内容不能含有：';
		foreach ($dict as $value){
			$msg .= $value . '&nbsp;&nbsp;';
		}
	} else {//开始发送短信
		if (is_array($phones)) { //群发短信
			$errorArray = array(
			'0'=>'发送成功进入审核阶段',
			'1'=>'直接发送成功',
			'-1'=>'帐号尚未注册',
			'-2'=>'其他错误',
			'-3'=>'帐号或者密码错误',
			'-4'=>'一次提交信息不能超过600个手机号码',
			'-5'=>'企业号帐户余额不足，请先充值再提交短信息！',
			'-6'=>'定时发送时间不是有效的时间格式',
			'-7'=>'发送短信内容包含黑字典关键字',
			'-8'=>'发送内容需在3到250个字之间',
			'-9'=>'发送号码为空'
			);
			$sendMsg = $sms->BatchSend($phones,$content);//发送
		}else {//单方发送
			$errorArray = array('0'=>'发送成功','-1'=>'帐号未注册','-2'=>'其他错误','-3'=>'密码错误','-4'=>'手机号格式不对','-5'=>'余额不足','-6'=>'定时发送时间不是有效的时间格式');
			$sendMsg = $sms->Send($phones,$content); //发送
		}
		addlog("发送短信", $_SESSION['user_name']);
		$backid = ($sendMsg == 0 or $sendMsg == 1) ? 1 : 0;
		unset($phones);
		foreach ($errorArray as $key => $value){
			if ($sendMsg == $key) {
				admin_showerrs($value, null, $backid );//提示信息
			}
		}
	}
}
?>
<style type="text/css">
.black_overlay{display: none;position: absolute;top: 0%;left: 0%;width: 100%;height: 100%;background-color:#FFFFFF;z-index:1001;-moz-opacity: 0.8;opacity:.80;filter: alpha(opacity=80);}
.white_content{display: none;position: absolute;top: 25%;left: 25%;width: 50%;height: 50%;padding: 10px;border: 10px solid #bbb;margin:-20px;background-color: white;z-index:1002;overflow: auto;}
</style>
<script language="javascript" type="text/javascript">
function moveselect(obj,target,all){
	if (!all) all=0
	if (obj!="[object]") obj=eval("document.all."+obj)
	target=eval("document.all."+target)
	if (all==0)
	{
		while (obj.selectedIndex>-1){
			//alert(obj.selectedIndex)
			mot=obj.options[obj.selectedIndex].text
			mov=obj.options[obj.selectedIndex].value
			obj.remove(obj.selectedIndex)
			var newoption=document.createElement("OPTION");
			newoption.text=mot
			newoption.value=mov
			target.add(newoption)
		}
	}
	else
	{
		//alert(obj.options.length)
		for (i=0;i<obj.length;i++)
		{
			mot=obj.options[i].text
			mov=obj.options[i].value
			var newoption=document.createElement("OPTION");
			newoption.text=mot
			newoption.value=mov
			target.add(newoption)
		}
		obj.options.length=0
	}
}
function dakai(){
	document.getElementById('light').style.display='block';
	document.getElementById('fade').style.display='block'
}
function guanbi(){
	//下面上把右边select的值传到文本框内
	var accepter=document.getElementById("phone")
	accepter.value=""//如果不加这句，则每次选择的结果追加
	var huoQu=document.getElementById("D2")
	for(var k=0;k<huoQu.length;k++)
	accepter.value=accepter.value + huoQu.options[k].value + ";"//这里为字符间的分隔符
	document.getElementById('light').style.display='none';
	document.getElementById('fade').style.display='none'
}
</script>
</head><body>
<table width="98%" border="1" align="center" cellpadding="5" cellspacing="0" class="table">
	<tr>
		<td>如果在发送短信时遇到什么问题或困难，请与<a href="http://www.22cn.cn" target="_blank">呜雷科技</a>联系</td>
	</tr>
</table>
<br>
<table width="98%" border="1" align="center" cellpadding="5" cellspacing="0" class="table">
<form action="Admin_Sms_Send.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
	<tr>
		<td width="10%" rowspan="3" align="right"><span style="color:red;">*</span> 接收手机号：</td>
		<td width="90%"><label> 多方发送时请用英文状态下的分号(<span style="color:red;"><b> ; </b></span>)分隔多个手机号码，不能出现空格、回车、换行等非法操作符； 手机号为11位数字，前面不要加0或86等字符串；一次最多只能发送<span style="color:red; font-weight:bolder;">600</span>个手机号码，多余号码系统将自动忽略；使用导入操作时必须使用本系统提供的导入模板
<br />
		<input name="phone" type="text" value="<?=$_POST['phone']?>" id="phone" style="width:500px;" />
		 <a href="javascript:void(0);" onClick="dakai();"><b>从号码库选择</b></a></label></td>
	</tr>
	<tr>
	  <td><input name="importfile" type="file" id="importfile">
	    你可以用Excel导入接收者手机号码，点击这里<a href="./module/SMS_IMPORT_TEMP.rar" style="color:red;">下载导入模板</a>，解压后请先查看包中的“说明.txt”文件</td>
    </tr>
	<tr>
	  <td>
	  <?php
	  foreach ($_classArray as $key => $value) {
	  ?>
	  <label><input name="class[]" type="checkbox" id="class" value="<?=$key?>"><?=$value?></label>
	  <?php
	  }
	  ?>
	  </td>
    </tr>
	<tr>
		<td align="right"><span style="color:red;">*</span> 短信内容：</td>
		<td><label>短信内容必需在3到60个字之间<br>
		<textarea name="content" rows="5" id="content" style="width:440px;"><?=$_POST['content']?></textarea> 
		<span style="color:red;"><br><?=$msg?></span>
		</label></td>
	</tr>
	<tr>
		<td>&nbsp;</td>
		<td><label>
		  <input name="action" type="hidden" id="action" value="send" />
		  <input type="submit" name="Submit" value="发 送" />
		</label>（由于网速及系统处理原因短信发送有可能出现延时现象，因此同一个手机号码请不要重复发送）</td>
	</tr>
		 </form>
</table>


<div id="light" class="white_content">
<table border="1" width="350" id="table4" bordercolor="#CCCCCC" bordercolordark="#CCCCCC" bordercolorlight="#FFFFFF" cellpadding="3" cellspacing="0">
  <tr>
    <td width="150" height="200" align="center" valign="middle">
     短信接收人员列表
      <select size="12" name="D1" ondblclick="moveselect(this,'D2')" multiple="multiple" style="width:140px">
        <?php       
        $db->query('SELECT * FROM tun2_phone WHERE acl=1');
        while (0<$db->next_record()) {
        	echo "<option value=\"".$db->Record['phone']."\">".$db->Record['name']."</option>";
        }
        ?>
      </select>
    </td>
    <td width="50" height="200" align="center" valign="middle">
      <input type="button" value="<<" name="B3" onClick="moveselect('D2','D1',1)" /><br />
      <input type="button" value="<" name="B5" onClick="moveselect('D2','D1')" /><br />
      <input type="button" value=">" name="B6" onClick="moveselect('D1','D2')" /><br />
      <input type="button" value=">>" name="B4" onClick="moveselect('D1','D2',1)" /><br />
    </td>
    <td width="150" height="200" align="center" valign="middle">
      当前已选中的短信接收人员
      <select size="12" name="D2" id="D2" ondblclick="moveselect(this,'D1')" multiple="multiple" style="width:140px">
        
      </select>
    </td>
  </tr>
</table>
&nbsp;<input type="button" name="button" onClick="guanbi()" value="确 定" /> <span>选择时按住CTRL键可同时选择多项</span>
</div>
<div id="fade" class="black_overlay"></div>
</body>
</html>